﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PKTools.Database;
using PKTools.Scheduler;
using ServiceStack.Text;

namespace Scheduler
{
    abstract class DatabaseExecutor
    {        
        protected readonly JobStep
            jobStep;

        protected readonly Alias
            alias;

        public DatabaseExecutor(JobStep jobStep)
        {
            this.jobStep = jobStep;
            this.alias = this.jobStep.Alias;
        }

        public abstract void Execute();

        public virtual void Alert()
        {
        }
    }

    class QueryProperties
    {
        public string
            SqlQuery { set; get; }
    }

    class QueryExecutor : DatabaseExecutor
    {
        readonly QueryProperties
            properties;

        public QueryExecutor(JobStep step) 
            : base(step)
        {
            this.properties = JsonSerializer.DeserializeFromString<QueryProperties>(this.jobStep.Properties);     
        }

        public override void Execute()
        {
            using (var sql = new MsSql(false, this.alias.Connection))
            {
                sql.AddInput("LastRun", this.jobStep.LastRun);
                sql.AddInput("LastSuccessfulRun", this.jobStep.LastSuccessfulRun);

                var 
                    list = sql.LoadDynamicList(this.properties.SqlQuery);
            }
        }
    }

    class SnapShotQueryExecutor : DatabaseExecutor
    {
        public SnapShotQueryExecutor(JobStep step) : base(step)
        {
        }

        public override void Execute()
        {            
        }
    }
}
